<!-- md5.js -->
<script type="text/javascript" src="/js/md5.min.js"></script>
<!-- common.js -->
<script type="text/javascript" src="/js/common.js"></script>
<style>
    .layui-form-item .layui-input-company {
        width: auto;
        padding-right: 10px;
        line-height: 38px;
    }
</style>
<div class="layuimini-container layuimini-page-anim">
  <div class="layuimini-main">

    <div class="layui-form layuimini-form">
      <div class="layui-form-item">
        <div class="layui-input-block">
          <input type="hidden" id="mobile" name="mobile" lay-verify="required" value="" class="layui-input">
        </div>
      </div>

      <div class="layui-form layuimini-form">
        <div class="layui-form-item">
          <label class="layui-form-label required">旧的密码</label>
          <div class="layui-input-block">
            <input type="password" name="old_password" lay-verify="required" lay-reqtext="旧的密码不能为空"
                   placeholder="请输入旧的密码"
                   value="" class="layui-input">
            <tip>填写自己账号的旧的密码。</tip>
          </div>
        </div>

        <div class="layui-form-item">
          <label class="layui-form-label required">新的密码</label>
          <div class="layui-input-block">
            <input type="password" name="password" lay-verify="required" lay-reqtext="新的密码不能为空" placeholder="请输入新的密码"
                   value="" class="layui-input">
          </div>
        </div>

        <div class="layui-form-item">
          <label class="layui-form-label required">确认密码</label>
          <div class="layui-input-block">
            <input type="password" name="ack_password" lay-verify="required" lay-reqtext="新的密码不能为空"
                   placeholder="请输入新的密码" value="" class="layui-input">
          </div>
        </div>

        <div class="layui-form-item">
          <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认</button>
          </div>
        </div>

      </div>
    </div>
  </div>
</div>
</html>

<script>
    layui.use(['form', 'miniPage'], function () {
        var form = layui.form,
            layer = layui.layer,
            miniPage = layui.miniPage,
            $ = layui.jquery;

        $.ajax({
            url: "/user/getMobile",
            type: "GET",
            success: function (data) {
                if (data.code == 200) {
                    $('#mobile').val(data.object);
                } else {
                    layer.msg(data.message);
                }
            }
        });

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var inputPass = data.field.password;
            var inputPass_ack = data.field.ack_password;
            if (inputPass === inputPass_ack) {
                var password = f_md5(inputPass);
                var password_old = f_md5(data.field.old_password);

            } else {
                layer.msg("两次输出密码不一致！请重新输入！", {time: 2000});
            }

            $.ajax({
                url: "/user/updateUserPwd",
                type: "POST",
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                data: JSON.stringify({
                    oldPwd: password_old,
                    mobile: data.field.mobile,
                    password: password
                }),
                success: function (data) {
                    if (data.code == 200) {
                        layer.msg("密码修改成功！");
                        setTimeout(function (){
                            location.reload()
                        }, 2000);
                    } else {
                        layer.msg(data.message);
                        layer.close(index);
                    }
                }
            });

            return false;
        });
    });
</script>